<?php
namespace PsiFramework\Applications\DefaultApp\Classes\Actions\User;

use PsiFramework\Classes\Util\UI\LanguageManager;

use PsiFramework\Classes\Factory\DAOFactory;

use PsiFramework\Classes\Util\HTTP\Request;
use PsiFramework\Classes\Util\HTTP\HTTPRequest;
use \PsiFramework\Classes\Actions\AbstractAction;
use \PsiFramework\Classes\Util\Object\Identification;

/**
 * PsiFramework
 * Php Simple Framework
 *
 * @author Rémi San
 * @version beta
 */

/**
 * The LostPassword Action
 *
 * This action is used when a user wants to retrieve his lost password
 *
 * @package Actions.User
 */
class LostPasswordAction extends AbstractAction
{

    /**
     * Function executed by the controller
     *
     * It shows the lost password form page or sends the new password
     *
     * @return Response the response to the action execution
     */
    public function execute()
    {
        parent::execute();
        
        $email = $this->_request->getParameter('email');
        if (isNullOrEmptyString($email)) {
            $this->findForward("form");
        } else {
            
            $userDao = DAOFactory::getDAO('User');
            $user = $userDao->retrieveByLoginOrEmail($email);
            
            $message = '';
            if (!is_null($user)) {
                $ok = $userDao->sendLostPasswordEmail($user);
                
                if ($ok) {
                    $message = 'MESSAGE_SENT'; 
                } else {
                    $message = 'ERROR_WHILE_SENDING_MESSAGE';
                }
            } else {
                $message = 'USER_NOT_FOUND';
            }
            
            $this->_response->setAttribute('message', LanguageManager::getInstance()->getText($message));
            $this->findForward("success");
        }
    }
}
